//
//  CZNetworkTool.swift
//  SinaWeiBo
//
//  Created by 刘幼寒 on 5/16/16.
//  Copyright © 2016 刘幼寒. All rights reserved.
//

import UIKit

import AFNetworking

//请求方法枚举
enum kREQUESTMETHOD: String
{
    case GET = "GET"
    case POST = "POST"
    
}

class CZNetworkTool: NSObject
{
    
    //单例
    static let sharedInstance: CZNetworkTool = CZNetworkTool()
    
    //定义一个AFHTTPSessionManager类型的属性值，用来调用AFN的一些方法
    private let afnManger: AFHTTPSessionManager = {
        let afn = AFHTTPSessionManager(baseURL: NSURL(string: "https://api.weibo.com/"))
        //给响应的序列化器多添加支持的类型
        afn.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return afn
    }()
    
    //包装AFN的网络请求方法
    func request(method: kREQUESTMETHOD, URLString: String, parameters: AnyObject?, progress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?)
    {
        if method == kREQUESTMETHOD.GET
        {
            afnManger.GET(URLString, parameters: parameters, progress: progress, success: success, failure: failure)
        }
        else
        {
            afnManger.POST(URLString, parameters: parameters, progress: progress, success: success, failure: failure)
        }
    }
    
}
